var gl_material_vue_vm_list_html = $('#material_vue_vm_list').html();

$(function() {
	$('#material_vue_vm_list div').hide();
	
	findAll();
});

var material = {
	materialId: '',
	materialName: '',
	brand: '',
	model: '',
	price: '',
	cost: '',
	stockNumber: '',
	iconUrl: '',
	intro: '',
	status: '',
	createTime: '',
	updateTime: '',
	createId: getLoginedUser()? getLoginedUser().staffId: '',
	updateId: getLoginedUser()? getLoginedUser().staffId: '',
	remark: '',
};

var view = {
	data: material
};

var vue_vm = new Vue({
	el: '#vue_vm',
	data: view
});

function save() {
	view.data.price = (view.data.price==''? 0: parseFloat(view.data.price));
	view.data.cost = (view.data.cost==''? 0: parseFloat(view.data.cost));
	view.data.stockNumber = (view.data.stockNumber==''? 0: parseInt(view.data.stockNumber));
	view.data.status = (view.data.status==''? 0: parseInt(view.data.status));

	save_basic(path+'/material/save', {material:view.data}, function() {
		alert('保存成功');
		findAll();
	});
}

function findAll() {
	if($('#material_vue_vm_list').length==0) {return;}
	$('#material_vue_vm_list div').hide();

	findAll_basic(path+'/material/findAll', function(response) {
		$('#material_vue_vm_list').html(gl_material_vue_vm_list_html);
		
		new Vue({
			el: '#material_vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				onClickMaterialRadiobox:function(todo) {
					var materialId = $('input:radio[name="materialId"]:checked').val();
					if(materialId==todo.materialId) {
						if(behalf_material_view) {
							behalf_material_view.data.price = todo.price;
						}
					}
				},
				update: function(todo) {
					todo.price = (todo.price==''? 0: parseFloat(todo.price));
					todo.cost = (todo.cost==''? 0: parseFloat(todo.cost));
					todo.stockNumber = (todo.stockNumber==''? 0: parseInt(todo.stockNumber));
					todo.status = (todo.status==''? 0: parseInt(todo.status));
				
					save_basic(path+'/material/save', {material:todo}, function() {
						alert('保存成功');
					});
				},
				deleteBatchById: function(materialId) {
					var itemIdArray = [materialId];
					
					deleteBatch_basic_by_array(path+'/material/deleteBatch', itemIdArray, function(response) {
						$('#'+materialId).remove();
					});
				},
				getImageFullPath: function(iconUrl) {
					return imagePath + '/' + iconUrl;
				}
			}
		});
		
		$('#material_vue_vm_list div').show();
	});
}

function deleteBatch() {
	deleteBatch_basic(path+'/material/deleteBatch');
}
